package edu.hawaii.duedates.webApp;

import java.util.List;
import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.PropertyModel;
import edu.hawaii.duedates.DueDates;
import edu.hawaii.duedates.ItemDue;

/**
 * The java implementation of the Result page of the DueDates
 * application. Page displays all books due for a given user in table
 * form. Class extends WebPage.
 * 
 * @author Tyler Wolff, Mari-Lee Flestado, Aric West
 */
public class ResultPage extends DueDatesPage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /**
   * Constructs the Result page of the DueDates web application.
   * 
   * Contains a listing of the Libraries defined for that user.  Each 
   * library has an associated "update" button that checks whether any 
   * books are due for that user and library, and if so, presents the information.
   * 
   * Page also contains a logout option which ends a user session.
   */
  public ResultPage () {
    
    
    // Add CSS definitions for this page
    add(HeaderContributor.forCss(
        edu.hawaii.duedates.webApp.DueDatesPage.class, "style/result.css", "screen"));  
    
    //goes to the result page when this link is clicked
    add(new Link("Results") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L; 
      @Override
      public void onClick() {
        setResponsePage(ResultPage.class);
      }
    });
    
    //goes to the alert page when this link is clicked
    add(new Link("Alerts") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L; 
      @Override
      public void onClick() {
        setResponsePage(AlertsPage.class);
      }
    });
    
    //goes to the login page when this link is clicked
    add(new Link("Logout") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L; 
      @Override
      public void onClick() {
        getDueDatesSession().invalidate();
        setResponsePage(Index.class);
      }
    });
    
    Form resultForm = new Form("resultForm");
    add(resultForm);

    WebMarkupContainer uhContainer = new WebMarkupContainer("uhContainer")  {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      /** Only visible if user has UH library credentials */
      @Override
      public boolean isVisible() {
        return getDueDatesSession().hasUh();
      }
    };
    
    uhContainer.add(new Label("uhLabel", "University of Hawaii at Manoa Library"));
    uhContainer.add(new TextField("withinUh", new PropertyModel(getSession(), "withinUh")));

    //add update button for UHM library for this user
    uhContainer.add(new Button("updateUh") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      /** Only visible if user has UH library credentials */
      @Override
      public boolean isVisible() {
        return getDueDatesSession().hasUh();
      }
      
      /** Calls out the UHM library system, retrieves and stores the results. */
      @Override
      public void onSubmit() {
        DueDates tempDueDates = getDueDatesSession().getDueDatesObject();
        Integer within = (getDueDatesSession().getWithinUh() == null ? 0 
            : Integer.valueOf(getDueDatesSession().getWithinUh()));
        List<ItemDue> uhmItems = tempDueDates.getItems("uhm");
        getDueDatesSession().setUhList(tempDueDates.itemsWithin(uhmItems, within));
        getDueDatesSession().getList().clear();
        
        // combines results
        if (!getDueDatesSession().getUhList().isEmpty()) {
          getDueDatesSession().getList().addAll(getDueDatesSession().getUhList());
        }
        if (!getDueDatesSession().getHslList().isEmpty()) {
          getDueDatesSession().getList().addAll(getDueDatesSession().getHslList());
        }
        setResponsePage(ResultPage.class);
      }
    });

    resultForm.add(uhContainer);
    
    WebMarkupContainer hslContainer = new WebMarkupContainer("hslContainer")  {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      /** Only visible if user has UH library credentials */
      @Override
      public boolean isVisible() {
        return getDueDatesSession().hasHsl();
      }
    };
    
    hslContainer.add(new Label("hslLabel", "Hawaii State Library"));
    hslContainer.add(new TextField("withinHsl", new PropertyModel(getSession(), "withinHsl")));

    //add update button for UHM library for this user
    hslContainer.add(new Button("updateHsl") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;

      /** Only visible if user has HSL credentials */
      @Override
      public boolean isVisible() {
        return getDueDatesSession().hasHsl();
      }
      
      /** Calls out the UHM library system, retrieves and stores the results. */
      @Override
      public void onSubmit() {

        DueDates tempDueDates = getDueDatesSession().getDueDatesObject();
        Integer within = (getDueDatesSession().getWithinHsl() == null ? 0 
            : Integer.valueOf(getDueDatesSession().getWithinHsl()));
        List<ItemDue> hslItems = tempDueDates.getItems("hsl");
        getDueDatesSession().setHslList(tempDueDates.itemsWithin(hslItems, within));
        getDueDatesSession().getList().clear();
        
        // combines results
        if (!getDueDatesSession().getUhList().isEmpty()) {
          getDueDatesSession().getList().addAll(getDueDatesSession().getUhList());
        }
        if (!getDueDatesSession().getHslList().isEmpty()) {
          getDueDatesSession().getList().addAll(getDueDatesSession().getHslList());
        }
        setResponsePage(ResultPage.class);
      }
    });

    resultForm.add(hslContainer);
    
    WebMarkupContainer wmc = new WebMarkupContainer("resultTable") {
      /** For serialization. */
      private static final long serialVersionUID = 1L;
      
      //table result is not visible if the result list is empty
      @Override
      public boolean isVisible() {
        return !getDueDatesSession().getList().isEmpty();
      }  
    };
    
    //Add link to library tab
    wmc.add(new Link("library") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;
          //sort by library if "library" is clicked
         @Override
         public void onClick() {
           getDueDatesSession().setList(getDueDates().itemSort(getDueDatesSession().getList(),
               "library"));
           setResponsePage(ResultPage.class);
         }
    });
      
    //sort by duedate if "duedate" tab is clicked
    wmc.add(new Link("duedate") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L; 
      @Override
      public void onClick() {
        getDueDatesSession().setList(getDueDates().itemSort(getDueDatesSession().getList(),
            "duedate"));
        setResponsePage(ResultPage.class);
      }
    });
      
    //sort by title if "title" tab is clicked
    wmc.add(new Link("title") {
      /** Support serialization. */
      private static final long serialVersionUID = 1L;
      @Override
      public void onClick() {
        getDueDatesSession().setList(getDueDates().itemSort(getDueDatesSession().getList(),
            "title"));
        setResponsePage(ResultPage.class);
      }
    });
    
    DataView dataView = new DataView("itemTable", 
        new ListDataProvider(getDueDatesSession().getList())) {
      
      /** For serialization. */
      private static final long serialVersionUID = 1L;
      //adding library name and update button
      /**
       * Display each row in the table.
       * 
       * @param item A item instance to be displayed.
       */
      @Override
      public void populateItem(Item item) {
        ItemDue itemDue = (ItemDue) item.getModelObject();
        item.add(new Label("Library", itemDue.getItemLib()));
        item.add(new Label("Title", itemDue.getItemName()));
        item.add(new Label("DueDate", itemDue.getItemDueDate()));
      }
    };
    // Add the dataview to the ResultPage. 
    wmc.add(dataView);
    resultForm.add(wmc);
  }
}